package com.cooper.scheduler;

import java.util.List;

import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.entity.SchedulerInfo;
import com.cooper.loader.Loader;
import com.cooper.service.core.SchedulerService;

@Service("schedulerLoader")
public class DefaultSchedulerLoader implements Loader {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SchedulerService schedulerService;
	
	@Autowired
	private Scheduler scheduler = null;
	
	@Override
	public void init() throws Exception {
		List<SchedulerInfo> schedulerList = this.schedulerService.queryForList();
		if(null != schedulerList){
			for(SchedulerInfo sched: schedulerList){
				this.schedulerService.addTask(sched);
			}
			scheduler.start();
		}
	}

	@Override
	public void destroy() throws Exception{
		if(null != this.scheduler){
			logger.info("Stop scheduler...");
			scheduler.shutdown();
		}
		
	}

}
